/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import main.ServicesList;
import model.system.SystemGroupAccess;
import model.system.SystemMenu;
import service.system.SystemGroupAccessService;
import utility.Constants;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class SystemMenuTM extends AbstractTableModel{
    static protected String[] columnNames = { "NO", "MODUL", "MENU NAME","TAMBAH","EDIT","HAPUS","PRINT","APPROVE","ID" };
    static protected Class<?>[] columnTypes = { Integer.class, String.class, String.class, Boolean.class,Boolean.class,Boolean.class,Boolean.class,Boolean.class,Integer.class };
     private List<SystemMenu> data;
     private int filterMenuId,filterGroupId;

    public SystemMenuTM() {
        this(new ArrayList<SystemMenu>(0));
    }

    public SystemMenuTM(List<SystemMenu> data) {
        this.data = data;
    }

    public SystemMenu get(int index) {
        return data.get(index);
    }

    public void add(SystemMenu product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, SystemMenu product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<SystemMenu> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        String menuPolicy = data.get(rowIndex).getPolicy();
        filterMenuId = data.get(rowIndex).getMenuId();
        if (menuPolicy==null) menuPolicy = "";
        
         
        String groupPolicy = "";
        
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getMenuGroup();
            case 2: return data.get(rowIndex).getMenuName();
            case 3: 
                
                if (gotPolicy(Constants.POLICY_ADD, menuPolicy, true)!=null){
                    return data.get(rowIndex).getAllowAction().isCanAdd();//  Boolean.TRUE;//gotPolicy("ADD;", menuPolicy, true);
                }else return null;
            case 4: 
                    if (gotPolicy(Constants.POLICY_EDIT, menuPolicy, true)!=null){
                        return data.get(rowIndex).getAllowAction().isCanEdit();
                    }//gotPolicy("EDIT;", menuPolicy, true);
                    else return null;
            case 5: if (gotPolicy(Constants.POLICY_DELETE, menuPolicy, true)!=null){
                        return data.get(rowIndex).getAllowAction().isCanDelete();
                    }////gotPolicy("DELETE;", menuPolicy, true);
                    else return null;
            case 6: if (gotPolicy(Constants.POLICY_PRINT, menuPolicy, true)!=null){
                        return data.get(rowIndex).getAllowAction().isCanPrint();
                    }////gotPolicy("PRINT;", menuPolicy, true);
                    else return null;
            case 7: if (gotPolicy(Constants.POLICY_APPROVAL, menuPolicy, true)!=null){
                            return data.get(rowIndex).getAllowAction().isCanApprove();
                        }//data.get(rowIndex).getMenuId();            
                    else return null;    
            case 8: return data.get(rowIndex).getMenuId();  
            default: return null;
        }
    }
    
     @Override
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
              String menuPolicy = data.get(rowIndex).getPolicy();
        if (menuPolicy==null) menuPolicy = "";
         
           switch (columnIndex) {
           //    case 2 : data.get(rowIndex).setMenuName(String.valueOf(aValue));// getMenuName();
            case 3: data.get(rowIndex).getAllowAction().setCanAdd((Boolean)aValue);break;// PolicyAction().setCanAdd((Boolean)aValue) ;
            case 4: data.get(rowIndex).getAllowAction().setCanEdit((Boolean)aValue);break;// PolicyAction().setCanEdit((Boolean)aValue) ;
            case 5: data.get(rowIndex).getAllowAction().setCanDelete((Boolean)aValue);break;// PolicyAction().setCanDelete((Boolean)aValue) ;
            case 6: data.get(rowIndex).getAllowAction().setCanPrint((Boolean)aValue);break;// PolicyAction().setCanPrint((Boolean)aValue) ;
            case 7: data.get(rowIndex).getAllowAction().setCanApprove((Boolean)aValue);break;// PolicyAction().setCanApprove((Boolean)aValue) ;
           }
         
        fireTableCellUpdated(rowIndex, columnIndex);
        }
     
    private Boolean gotPolicy(String policyName,String menuPolicy, boolean key){
        Boolean rs;
      
            if (menuPolicy.indexOf(policyName)>-1){
               rs = Boolean.valueOf(key);// (key? Boolean.TRUE: Boolean.FALSE);
            }
            else rs = null;
        
        
         return rs;//Boolean.TRUE;
    }
    
     @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }
     
     @Override
    public Class getColumnClass(int c) {
       //  return columnTypes[c];
            //return getValueAt(0, c).getClass();
        Object value=this.getValueAt(0,c);  
        return (value==null?Object.class:value.getClass());  
    }
//     
    @Override
     public boolean isCellEditable(int row, int col) {
        //Note that the data/cell address is constant,
        //no matter where the cell appears onscreen.
       
        if ((col >2)&&(col<8)) {
            return true;
        } else {
            return false;
        }
    }

    public int getFilterGroupId() {
        return filterGroupId;
    }

    public void setFilterGroupId(int filterGroupId) {
        this.filterGroupId = filterGroupId;
    }

    public int getFilterMenuId() {
        return filterMenuId;
    }

    public void setFilterMenuId(int filterMenuId) {
        this.filterMenuId = filterMenuId;
    }
    
    

}
